package com.example.demo.config;

import com.example.demo.interceptor.AddUserInterceptor;
import com.example.demo.service.BookService;
import com.example.demo.service.UserService;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

@EnableWebMvc
@Configuration
public class MvcConfiguration implements WebMvcConfigurer {
    private final BookService bookService;
    private final UserService userService;

    public MvcConfiguration(BookService bookService, UserService userService) {
        this.bookService = bookService;
        this.userService = userService;
    }
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }

    @Bean
    public AddUserInterceptor addUserInterceptor() {
        return new AddUserInterceptor(bookService, userService);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(addUserInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns("/login");
    }
}
